package com.zh.api.controller.goods;


import com.alibaba.fastjson.JSONObject;
import com.zh.common.domain.Tree;
import com.zh.common.utils.PageUtils;
import com.zh.common.utils.Query;
import com.zh.common.utils.ResultVO;
import com.zh.goods.domain.GoodsTypeDO;
import com.zh.goods.service.GoodsTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-12-12 09:50:30
 */
 
@Controller
@RequestMapping("/goods/goodsType")
@Api("商品类型管理")
public class GoodsTypeController {
	@Autowired
	private GoodsTypeService ocGoodsTypeService;

	/**
	 * 获取商品类型
	 * @param params
	 * @return
     */
	@ResponseBody
	@RequestMapping(value = "list", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation("获取商品类型列表")
	public PageUtils list(@RequestParam Map<String, Object> params){
		String  s = (String) params.get("s");
		String[] idAll=s.split(",");
		System.out.println(idAll);
		params.put("ss",idAll);
        Query query = new Query(params);
		List<GoodsTypeDO> ocGoodsTypeList = ocGoodsTypeService.list(query);
		int total = ocGoodsTypeService.count(query);
		PageUtils pageUtils = new PageUtils(ocGoodsTypeList, total);
		return pageUtils;
	}


	@RequestMapping(value = "getGoodsType", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("获取商品类型详情")
	ResultVO getGoodsType(@RequestBody GoodsTypeDO goodsTypeDO){
		GoodsTypeDO ocGoodsType = ocGoodsTypeService.get(goodsTypeDO.getId());
	    return ResultVO.success(ocGoodsType);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping(value = "save", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation("保存商品类型")
	public ResultVO save(@RequestBody GoodsTypeDO ocGoodsType){
		if(ocGoodsTypeService.save(ocGoodsType)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping(value = "update", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation("修改商品类型")
	public ResultVO update(@RequestBody GoodsTypeDO ocGoodsType){
		if(ocGoodsTypeService.update(ocGoodsType)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value = "remove", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("删除商品类型")
	public ResultVO remove(@RequestBody GoodsTypeDO ocGoodsType){
		if(ocGoodsTypeService.remove(ocGoodsType.getId())>0){
		return ResultVO.success();
		}
		return ResultVO.error();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value = "batchRemove", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("批量删除商品类型")
	public ResultVO batchRemove(@RequestBody JSONObject jsonParam){
		String[] ids= (String[]) jsonParam.get("ids");
		if(ocGoodsTypeService.batchRemove(ids)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}


	@RequestMapping(value = "tree", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("获取商品类型树")
	public ResultVO tree() {
		Tree<GoodsTypeDO> tree = new Tree<GoodsTypeDO>();
		tree = ocGoodsTypeService.getTree();
		return ResultVO.success(tree);
	}


	/**
	 * 获取所有
	 * @return
     */
	@ResponseBody
	@RequestMapping(value = "type", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation("获取商品所有类型")
	public ResultVO fun(){
		List<GoodsTypeDO> all = ocGoodsTypeService.getAll();
		return ResultVO.success(all);
	}
	
}
